import Platform, {PlatformType} from "./Platform";

//数据存储类
export default class DataStorage{

    //判断特定dataName数据是否存在
    public static dataExist(dataName):boolean
    {
        if(cc.sys.platform === cc.sys.Platform.WECHAT_GAME || cc.sys.platform===cc.sys.Platform.BYTEDANCE_MINI_GAME)
        {
            let data = wx.getStorageSync(dataName);
            if (data.length === 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else if(Platform.currentPlatform===PlatformType.VIVO)
        {
            let data=qg.getStorageSync({key:dataName});
            if (data.length === 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            let data = cc.sys.localStorage.getItem(dataName);
            if (data === null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        return false;
    }

    //保存数据
    public static save(dataName, data)
    {
        if(cc.sys.platform === cc.sys.Platform.WECHAT_GAME  || cc.sys.platform===cc.sys.Platform.BYTEDANCE_MINI_GAME)
        {
            wx.setStorageSync(dataName, data);
        }
        else if(Platform.currentPlatform===PlatformType.VIVO)
        {
            qg.setStorageSync({key:dataName, value: JSON.stringify(data)});
        }
        else
        {
            cc.sys.localStorage.setItem(dataName, JSON.stringify(data));
        }
    }

    //删除数据
    public static delete(dataName)
    {
        if (cc.sys.platform === cc.sys.Platform.WECHAT_GAME  || cc.sys.platform===cc.sys.Platform.BYTEDANCE_MINI_GAME)
        {
            wx.removeStorageSync(dataName);
        }
        else if(Platform.currentPlatform===PlatformType.VIVO)
        {
            qg.deleteStorageSync({ key: dataName});
        }
        else
        {
            cc.sys.localStorage.removeItem(dataName);
        }
    }

    public static deleteAll()
    {
        if (cc.sys.platform === cc.sys.Platform.WECHAT_GAME || cc.sys.platform===cc.sys.Platform.BYTEDANCE_MINI_GAME)
        {
            
        } else
        {
            cc.sys.localStorage.clear();
        }
    }

    //加载数据
    public static load(dataName,defaultValue = null)
    {
        if(cc.sys.platform === cc.sys.Platform.WECHAT_GAME || cc.sys.platform===cc.sys.Platform.BYTEDANCE_MINI_GAME)
        {
            let data = wx.getStorageSync(dataName);
            if (data.length !== 0)
            {
                return data;
            }
        }
        else if(Platform.currentPlatform===PlatformType.VIVO)
        {
            let data = qg.getStorageSync({key: dataName});
            if (data.length !== 0)
            {
                return JSON.parse(data);
            }
        }
        else
        {
            let dataStr = cc.sys.localStorage.getItem(dataName);
            //console.log("** DATA STR="+dataStr);
            if (dataStr !== null)
            {
                let data = JSON.parse(dataStr);
                return data;
            }
        }
        return defaultValue;
    }
}
